﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ClientManager.Data.Infrastucture;
using ClientManager.Model;

namespace ClientManager.Service.Clients
{
    public class ClientService : IClientService
    {
        private IUnitOfWork _unitOfWork { get; set; }
        private IRepository<Client> _clientRepository { get; set; }


        public ClientService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _clientRepository = _unitOfWork.GetRepository<Client>();
        }

        public void Create(Client entity)
        {
            _clientRepository.Add(entity);
            _unitOfWork.Commit();
        }

        public void Update(Client entity)
        {
            var clientOriginal = GetById(entity.ClientId);
            clientOriginal.FirstName = entity.FirstName;
            clientOriginal.Surname = entity.Surname;
            clientOriginal.ServiceLevelId = entity.ServiceLevelId;
            _clientRepository.Update(clientOriginal);
            _unitOfWork.Commit();

        }

        public void Delete(Client entity)
        {
            _clientRepository.Delete(entity);
            _unitOfWork.Commit();

        }


        public Client GetById(int id, params Expression<Func<Client, object>>[] includes)
        {
            return _clientRepository.Get(x => x.ClientId == id, includes);
        }

      
        public IEnumerable<Client> GetAll(params Expression<Func<Client, object>>[] includes)
        {
            return _clientRepository.GetAll(includes);
        }
    }
}
